<template>
  <div>
    <!-- 轮播上传 -->
    <el-upload
      ref="upload"
      :http-request="upload"
      :before-upload="beforeAvatarUpload"
      :limit="1"
      :file-list="fileList"
      :auto-upload="true"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      class="upload-demo"
      action="''"
      list-type="picture">
      <el-button size="mini" type="primary" round>更新图片</el-button>
    </el-upload>
  </div>
</template>

<script>

import { createId } from '@/utils/index' // 一个生产唯一的id的方法
import OSS from 'ali-oss'
// import { upload } from '@/api/promotion/imageSpace'
import { selectOSSInfo } from '@/api/set/oss'
export default {
  props: ['index'],
  data() {
    return {
      uploadParams: {
        uploadPath: '',
        uploadSize: ''
      },
      fileList: [],
      status: true,
      ossData:{
        region:'',// 服务器集群地区
        accessKeyId:'',// OSS帐号
        accessKeySecret:'',// OSS 密码
        bucket:'',// 阿里云上存储的 Bucket
      }
    }
  },
  created() {
    selectOSSInfo().then(res=>{
      if(res.code==1){
        // res.data=JSON.parse(this.$base64.decode(res.data));
        this.ossData.region=res.data.oss_region;
        this.ossData.accessKeyId=res.data.oss_accessKeyId;
        this.ossData.accessKeySecret=res.data.oss_accessKeySecret;
        this.ossData.bucket=res.data.oss_backetName;
      }
  
    })
  },
  methods: {
    // 图片上传前验证
    beforeAvatarUpload(file) {
      // file.size / 1024 图片的大小
      const format = /^image\/(jpeg|png|gif|bmp|jpg)$/.test(file.type);
      const isLt1M = file.size / 1024 / 1024 < 1
      this.uploadParams.uploadSize = file.size
      if (!format) {
        this.$message.error('上传图片只能是 JPG，PNG，GIF格式!')
      }
      if (!isLt1M) {
        this.$message.error('上传图片大小不能超过 1MB!')
      }
      return format && isLt1M
    },
    // 上传图片到OSS 同时派发一个事件给父组件监听
    upload(item) {
      let type
      const client = new OSS({
       region: this.ossData.region,
       accessKeyId: this.ossData.accessKeyId,
       accessKeySecret: this.ossData.accessKeySecret,
       bucket: this.ossData.bucket
      })
      switch (item.file.type) {
        case 'image/jpeg': {
          type = 'jpeg'
        } break
        case 'image/jpg': {
          type = 'jpg'
        } break
        case 'image/gif': {
          type = 'gif'
        } break
        case 'image/png': {
          type = 'png'
        } break
      }
      const key = 'fkgshops/shopid=' + this.$store.getters.shopId + '&time=' + new Date().getTime() + '&id=' + this.guidGenerator() + '.' + type// 存储路径，并且给图片改成唯一名字
      return client.put(key, item.file).then(res => { // OSS上传
        const url = res.url
        this.uploadParams.uploadPath = client.signatureUrl(key, { expires: 999999999999999 }) // 可访问的链接
        this.$refs.upload.submit()
      })
    },
    guidGenerator() {
      var S4 = function() {
        return ((1 + Math.random()) * 0X10000 | 0).toString(16).substring(1)
      }
      return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
    },
    handleSuccess(res, file) {
      this.fileList = []
      this.$emit('changeSrc', this.uploadParams.uploadPath, this.index)
    },
    // submitUpload() {
    //     this.$refs.upload.submit();
    // },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    }
  }
}
</script>

<style scoped ref="stylesheet/scss" lang="scss">
    .upload-demo{
        line-height: 28px;
    }
</style>
